<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jedrzej\Pimpable\PimpableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

class ProductLog extends Model
{
    use HasFactory;
    use PimpableTrait;

    /**
     * 表中的类型状态
     */
    //参与的活动类型
    public $typeTypeEnum = [
        'noActivity' => '普通商品',
        'seckill' => '秒杀',
        'bargain' => '砍价',
        'combination' => '拼团'
    ];

    /**
     * 可搜索字段
     */
    public $searchable = [
        'type',
        'product_id',
        'user_id',
        'pay_user_id',
        'created_at',
    ];

    /**
     * 可批量赋值字段 无
     */
    public $fillable = [
        'pay_user_id',
        'product_id',
        'user_id',
        'visit_num',
        'cart_num',
        'order_num',
        'pay_num',
        'pay_price',
        'cost_price',
        'refund_num',
        'refund_price',
        'collect_num',
        'type',
        'margin',
        'payment',
    ];

    /**
     * 可排序字段
     */
    public $sortable = [
        'visit_num',
        'id',
        'visitor_num',
        'cart_num',
        'order_num',
        'pay_price',
        'margin',
        'payment',
    ];
    /**
     * 查询字段模式
     *
     *  nickname => '%field%' 模糊查询
     *  created_at= (ge)field
     * @var array
     */
    public $searchableModels = [
        'created_at' => '%field%',
        'updated_at' => '%field%',
        'visit_num',
        'visitor_num',
        'cart_num',
        'order_num',
        'pay_price',
        'margin',
        'payment',

    ];
    /**
     * 默认排序字段
     */
    //protected $defaultSortCriteria = ['id,desc'];

    /**
     * 关联的商品
     */
    function product(){
        return $this->belongsTo(Product::class,'product_id','id');
    }

    /**
     * 关联的用户
     */
    function user(){
        return $this->belongsTo(User::class,'user_id','id');
    }

    /**
     * 关联的支付用户
     */
    function payUser(){
        return $this->belongsTo(User::class,'pay_user_id','id');
    }
}
